import parser
import os
from .base import Base

class TestOpset(Base):
    def __init__(self, success_list, failed_list):
        print('TestOpset Init--------------------------')
        self.tool_path = os.path.join(parser.get_value('maca_converter_root'), parser.get_value('maca_converter_path'))
        self.model_path = os.path.join(parser.get_value('model_root'), parser.get_value('opset_model_path'))
        self.success_list = success_list
        self.failed_list = failed_list

    def test(self):
        cwd = os.getcwd()
        output = os.path.join(cwd, 'tmp.onnx')
        version = 13

        os.chdir(self.tool_path)

        cmd = 'python ' + 'model_convert.py' + ' --model_path ' + self.model_path + ' --model_type onnx ' + '--output ' + output + ' --op_set ' + str(version)
        
        print('test_opset:', cmd)

        os.system(cmd)

        os.chdir(cwd)

        log_file = open('./convert.log','r')
        content = log_file.readline()

        if self.success_flag in content:
            params = {}
            params['inference'] = True
            params['version'] = version
            self.analyze_and_inference('opset', 'opset', output, self.success_list, self.failed_list, **params)      
        else:
            f = {}
            f['case'] = 'opset'
            f['phase'] = 'convert'
            self.failed_list.append(f)     

        log_file.close()
